<?
class shanhaiguan{
	public function __construct($UUlid){
		$this->client = new SoapClient('http://www.shg.com.cn:9092/com.dongrensm.shgpw/SupplierDataExchangeServiceSOAP?wsdl');
		switch($UUlid){
			case 10091:
				$this->user ='test001'; 
				$this->password ='aaaa1111';
			break;
			case 10180:
				$this->user ='qhdbaihe'; 
				$this->password ='bh921118';
			break;
			
			default:
			break;
		}
		$this->sign = $this->sign();
	}
	
	
	//【订单】创建订单（支付后下单）
	public function createOrder($request_array){
		$time = date('Y-m-d H:i:s');
		$productName = $request_array['UUltitle'].$request_array['UUttitle'];
		$request_xml =<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<request>
	<header>
		<application>shgpw.Menpiao.shgpw</application>
		<processor>SupplierDataExchangeProcessor</processor>
		<version>V1.0.0</version>
		<bodyType>ProductInfoRequestBody</bodyType>
		<createUser>{$this->user}</createUser>
		<createTime>{$time}</createTime>
		<signed>{$this->sign}</signed>
	</header>
	<body>
	
	<orderInfo>
		<orderId>{$request_array['pftordernum']}</orderId>
		<paymentType>{$request_array['ispay']}</paymentType>
		<product>
			<resourceId>{$request_array['uuid']}</resourceId>
			<productName>{$productName}</productName>
			<visitDate>{$request_array['UUplaytime']}</visitDate>
			<sellPrice>{$request_array['UUlprice']}</sellPrice>
			<sellquantity>{$request_array['UUtnum']}</sellquantity>
			<childPrice></childPrice>
			<childquantity></childquantity>
			<otherPrice></otherPrice>
			<otherquantity></otherquantity>
		</product>
		<contactPerson>
			<name>{$request_array['pftordername']}</name>
			<mobile>{$request_array['UUordertel']}</mobile>
			<email>test@shgpw.com</email>
			<credentials>{$request_array['IDcar']}</credentials>
			<credentialsType>ID_CARD</credentialsType>
		</contactPerson>
		<visitPerson>
			<person>
				<name>{$request_array['pftordername']}</name>
				<mobile>{$request_array['UUordertel']}</mobile>
				<credentials>{$request_array['IDcar']}</credentials>
				<credentialsType>ID_CARD</credentialsType>
			</person>
		</visitPerson>
		<orderRemark></orderRemark>	
	</orderInfo>
	 </body>
</request>
XML;
		// $this->logfile($request_xml);
		// print_R($request_xml);exit;
		$request_post = $this->client->createOrder(array("in"=>$request_xml))->out;
		$request_post_out = simplexml_load_string($request_post);
		// print_r($request_post_out);
		if((int)$request_post_out->header->code == 1000){
			return (string)$request_post_out->order->partnerorderId;
		}
		else{
			$this->logfile(date('Y-m-d H:i:s').'---'.$request_array['pftordernum']."->".(int)$request_post_out->header->code);
			return 101;
		}
	}
	
	//【订单】用户向门票供应商（我方）申请订单取消
	public function applyOrderRefundByUser($modify_array){
		// $this->logfile(json_encode($modify_array));
		$time = date('Y-m-d H:i:s');
		$request_xml =<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<request>
	<header>
		<application>shgpw.Menpiao.shgpw</application>
		<processor>SupplierDataExchangeProcessor</processor>
		<version>String</version>
		<bodyType>String</bodyType>
		<createUser>{$this->user}</createUser>
		<createTime>{$time}</createTime>
		<signed>{$this->sign}</signed>
	</header>
	<body>
		<orderInfo>		
			<partnerorderId>{$modify_array['partnerorderId']}</partnerorderId>
			<refundSeq></refundSeq>
			<orderMoney>{$modify_array['orderMoney']}</orderMoney>
			<orderQuantity>{$modify_array['orderQuantity']}</orderQuantity>
			<refundQuantity>{$modify_array['refundQuantity']}</refundQuantity>
			<refundReason></refundReason>
			<refundExplain></refundExplain>
		</orderInfo>
	</body>
</request>
XML;
		// $this->logfile($request_xml);
		$request_post = $this->client->applyOrderRefundByUser(array("in"=>$request_xml))->out;
		$request_post_out = simplexml_load_string($request_post);
		if((int)$request_post_out->header->code == 1000){
			return 100;
		}
		else{
			$this->logfile(date('Y-m-d H:i:s').'---'.$request_array['pftordernum']."->".(int)$request_post_out->header->code);
			return 101;
		}
	}
	
	
	
	//【订单】旅游网站向门票供应商（我方）查询订单状态
	 public function checkCreateOrder(){
		$time = date('Y-m-d H:i:s');
		$request_xml=<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<request>
	<header>
		<application>shgpw.Menpiao.shgpw</application>
		<processor>SupplierDataExchangeProcessor</processor>
		<version>String</version>
		<bodyType>String</bodyType>
		<createUser>$this->user</createUser>
		<createTime>$time</createTime>
		<signed>$this->sign</signed>
	</header>
	<body>
		<order>
			<partnerOrderId>pzp0707002</partnerOrderId>
		</order>
	</body>
</request>
XML;
		$request_post = $this->client->checkCreateOrder(array("in"=>$request_xml))->out;
		$request_post_out = simplexml_load_string($request_post);
		print_r($request_post_out);
	 }
	// 签名
	public function sign(){
		$sign = md5(md5($this->password).date('Ymd'));
		return $sign;
	}
	
	//错误日志
	public function logfile($txt){
		$fp = fopen('/var/www/html/new/d/module/api/ShanHaiGuan/shanhaiguan_logfile.txt',"a");
		flock($fp, LOCK_EX) ;
		fwrite($fp,$txt."\n");
		flock($fp, LOCK_UN);
		fclose($fp); 
	}
}
// $shg = new shanhaiguan(10091);
// $createOrder = $shg->createOrder();
// $applyOrderRefundByUser = $shg->applyOrderRefundByUser();
// $checkCreateOrder = $shg->checkCreateOrder();
?>